<template>
  <XtxDialog :title="formData.id?'修改收货地址':'添加收货地址'" v-model:visible="visibleDialog">
    <div class="address-edit">
         <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity :fullLocation="formData.fullLocation" @change="changeCity" placeholder="请选择所在地区"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </div>
    </div>
    <template #footer>
      <XtxButton @click="visibleDialog=false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup (props, { emit }) {
    const visibleDialog = ref(false) // 添加编辑是否显示
    function open (address) {
      visibleDialog.value = true
      if (address.id) {
        // 修改
        for (const key in address) {
          formData.value[key] = address[key]
        }
      } else {
      // 添加
        for (const key in formData.value) {
          if (key === 'isDefault') { formData.value[key] = 1 } else { formData.value[key] = null }
        }
      }
    }
    const formData = ref({
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 1,
      fullLocation: ''
    })
    // 选择城市
    function changeCity (result) {
      formData.value.provinceCode = result.provinceCode
      formData.value.cityCode = result.cityCode
      formData.value.countyCode = result.countyCode
      formData.value.fullLocation = result.fullLocation
    }
    // 确认
    function submit () {
      // 其实这里要先校验
      if (formData.value.id) {
        // 修改
        editAddress(formData.value).then(() => {
          Message({ type: 'success', text: '收货地址已修改' })
          visibleDialog.value = false
          emit('on-success', formData.value)
        }).catch(e => {
          Message({ type: 'error', text: '请检查信息是否完整' })
        })
      } else {
        // 添加
        addAddress(formData.value).then(data => {
          formData.value.id = data.result.id
          Message({ type: 'success', text: '收货地址已添加' })
          visibleDialog.value = false
          emit('on-success', formData.value)
        }
        ).catch(e => {
          Message({ type: 'error', text: '请检查信息是否完整' })
        })
      }
    }
    return { visibleDialog, open, formData, changeCity, submit }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}

</style>
